package com.homedo.odin.solution.service.bizservice.mng;

import com.homedo.core.common.base.dto.Meta;
import com.homedo.core.common.exception.BizException;
import com.homedo.core.common.util.NumberHelper;
import com.homedo.odin.solution.contract.mng.BrandFirmsEntranceResp;
import com.homedo.odin.solution.contract.mng.SolutionRankListReq;
import com.homedo.odin.solution.contract.mng.SolutionRankListResp;
import com.homedo.odin.solution.contract.mng.SolutionRankReq;
import com.homedo.odin.solution.dao.SolutionRankMapper;
import com.homedo.odin.solution.enumcode.basic.DeleteEnum;
import com.homedo.odin.solution.enumcode.basic.ErrorCodeEnum;
import com.homedo.odin.solution.enumcode.solution.RankTypeEnum;
import com.homedo.odin.solution.enumcode.solution.SolutionExpTypeEnum;
import com.homedo.odin.solution.model.dto.BrandOwnerBean;
import com.homedo.odin.solution.model.entity.Solution;
import com.homedo.odin.solution.model.entity.SolutionRank;
import com.homedo.odin.solution.service.BrandOwnerServiceImpl;
import com.homedo.odin.solution.service.BrandTypeServiceImpl;
import com.homedo.odin.solution.service.CommonTypeDataServiceImpl;
import com.homedo.odin.solution.service.SolutionRankServiceImpl;
import com.homedo.odin.solution.service.SolutionServiceImpl;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author: liyi
 * @date: 2018/5/17 17:36
 * @Description:
 */
@Service
public class MngSolutionRankBizService {


    @Autowired
    private BrandOwnerServiceImpl brandOwnerService;

    @Autowired
    private CommonTypeDataServiceImpl commonDataService;

    @Autowired
    BrandTypeServiceImpl brandTypeService;

    @Autowired
    SolutionRankServiceImpl solutionRankService;

    @Autowired
    SolutionServiceImpl solutionService;

    @Autowired
    SolutionRankMapper baseMapper;

    /**
     * @Description: 通过排行类型获取热搜精品列表
     * @Author: liyi
     */
    public SolutionRankListResp querySolutionRank(Integer rankType) {
        SolutionRankListResp solutionRankListResp = new SolutionRankListResp();
        List<SolutionRank> solutionRanks = solutionRankService.querySolutionRank(rankType);
        solutionRankListResp.setList(solutionRanks);
        //获取最新的修改记录
        SolutionRank lateSolutionRank = solutionRankService.queryLateSolutionRank(rankType);
        if (lateSolutionRank != null) {
            solutionRankListResp.setModifiedTime(lateSolutionRank.getModifiedTime());
            solutionRankListResp.setOperator(lateSolutionRank.getOperator());
            solutionRankListResp.setModifiedBy(lateSolutionRank.getModifiedBy());
        }
        return solutionRankListResp;
    }

    /**
     * @Description: 通过排行类型获取热搜列表
     * @Author: liyi
     */
    public SolutionRankListResp querySolutionRankHot() {
        SolutionRankListResp solutionRankListResp = new SolutionRankListResp();
        List<SolutionRank> solutionRanks = solutionRankService.querySolutionRankHot(RankTypeEnum.RESOU.getDbValue());
        solutionRankListResp.setList(solutionRanks);
        //获取最新的修改记录
        SolutionRank lateSolutionRank = solutionRankService.queryLateSolutionRank(RankTypeEnum.RESOU.getDbValue());
        if (lateSolutionRank != null) {
            solutionRankListResp.setModifiedTime(lateSolutionRank.getModifiedTime());
            solutionRankListResp.setOperator(lateSolutionRank.getOperator());
            solutionRankListResp.setModifiedBy(lateSolutionRank.getModifiedBy());
        }
        return solutionRankListResp;
    }


    /**
     * @Description: OMS_批量修改方案热搜表
     * @Author: liyi
     */
    public List<Meta> updateSolutionRank(SolutionRankListReq req) {
        /*
         * 操作步骤
         * 第一步把全部记录都变成不可用，
         * 第二步
         * 当方案UID没变时，把原来相同方案UID的变成可用，并更新对应状态。
         * 当方案UID变了，添加一条新数据
         */
        SolutionRank updateSolutionRank = new SolutionRank();
        updateSolutionRank.setRankType(RankTypeEnum.RESOU.getDbValue());
        updateSolutionRank.setIsDel(DeleteEnum.IS_DEL.getDbValue());
        updateSolutionRank.setModifiedBy(req.getModifiedBy());
        updateSolutionRank.setOperator(req.getOperator());
        baseMapper.updateSolutionRankStatusByType(updateSolutionRank);

        List<Meta> metaList = new ArrayList<Meta>();
        if (CollectionUtils.isNotEmpty(req.getList())) {
            List<SolutionRankReq> solutionRanks = req.getList();
            for (SolutionRankReq solutionRankReq : solutionRanks) {
                Solution solution = solutionService.findBySolutionId(solutionRankReq.getSolutionId());
                //如果方案为空，则不存该方案
                if (solution == null) {
                    metaList.add(new Meta(ErrorCodeEnum.NotExist.getDbValue(), "不存在该方案"));
                    continue;
                }
                SolutionRank querySolutionRank = new SolutionRank();
                querySolutionRank.setSolutionId(solutionRankReq.getSolutionId());
                querySolutionRank.setRankType(RankTypeEnum.RESOU.getDbValue());
                SolutionRank solutionRankResult = baseMapper.querySingleSolutionRank(querySolutionRank);
                //当solutionId没变
                if (solutionRankResult != null) {
                    SolutionRank solutionRank = solutionRankResult;
                    solutionRank.setId(solutionRankResult.getId());
                    solutionRank.setSolutionId(solutionRankReq.getSolutionId());
                    solutionRank.setRankType(solutionRankReq.getRankType());
                    solutionRank.setIsDel(DeleteEnum.NOT_DEL.getDbValue());
                    solutionRank.setModifiedTime(new Date());
                    solutionRank.setModifiedBy(solutionRankReq.getModifiedBy());
                    solutionRank.setOperator(solutionRankReq.getOperator());
                    solutionRankService.updateById(solutionRank);
                } else {
                    //当solutionId变了
                    if (NumberHelper.validatePositive(solutionRankReq.getSolutionId())) {
                        SolutionRank solutionRank = new SolutionRank();
                        solutionRank.setSolutionId(solutionRankReq.getSolutionId());
                        solutionRank.setRankType(solutionRankReq.getRankType());
                        solutionRank.setIsDel(DeleteEnum.NOT_DEL.getDbValue());
                        solutionRank.setCreatedBy(solutionRankReq.getModifiedBy());
                        solutionRank.setCreatedTime(new Date());
                        solutionRank.setModifiedTime(new Date());
                        solutionRank.setModifiedBy(solutionRankReq.getModifiedBy());
                        solutionRank.setOperator(solutionRankReq.getOperator());
                        solutionRankService.insert(solutionRank);
                    }
                }

            }
        }
        return metaList;
    }

    /**
     * @Description: OMS_批量修改精品方案
     * @Author: liyi 操作步骤 判断方案是否收费的，收费的提示不能保存 第一步把全部记录都变成不可用， 第二步 当方案UID没变时，把原来相同方案UID的变成可用，并更新对应状态。 当方案UID变了，添加一条新数据
     */
    public List<Meta> updateSolutionExtractiveRank(SolutionRankListReq req) {

        List<SolutionRankReq> solutionRanks = req.getList();
        solutionRanks.forEach(solutionRankReq -> {
            Solution solution = solutionService.findBySolutionId(solutionRankReq.getSolutionId());
            if (solution != null) {
                if (!SolutionExpTypeEnum.FREE.getDbValue().equals(solution.getIsFree())) {
                    throw new BizException(Meta.CODE_451.getCode(), "收费方案不能保存");
                }
            }
        });

        SolutionRank updateSolutionRank = new SolutionRank();
        updateSolutionRank.setRankType(RankTypeEnum.JINGPING.getDbValue());
        updateSolutionRank.setIsDel(DeleteEnum.IS_DEL.getDbValue());
        updateSolutionRank.setModifiedTime(new Date());
        updateSolutionRank.setOperator(req.getOperator());
        updateSolutionRank.setModifiedBy(req.getModifiedBy());

        baseMapper.updateSolutionRankStatusByType(updateSolutionRank);

        List<Meta> metaList = new ArrayList<Meta>();
        if (CollectionUtils.isNotEmpty(req.getList())) {
            solutionRanks = req.getList();
            for (SolutionRankReq solutionRankReq : solutionRanks) {
                Solution solution = solutionService.findBySolutionId(solutionRankReq.getSolutionId());
                //如果方案为空，则不存该方案
                if (solution == null) {
                    metaList.add(new Meta(ErrorCodeEnum.NotExist.getDbValue(), "不存在该方案"));
                    continue;
                }
                //如果是收费方案，则不存
                if (!SolutionExpTypeEnum.FREE.getDbValue().equals(solution.getIsFree())) {
                    continue;
                }
                SolutionRank querySolutionRank = new SolutionRank();
                querySolutionRank.setSolutionId(solutionRankReq.getSolutionId());
                querySolutionRank.setRankType(RankTypeEnum.JINGPING.getDbValue());
                SolutionRank solutionRankResult = baseMapper.querySingleSolutionRank(querySolutionRank);
                // 当solutionId没变,更新原来热搜
                if (solutionRankResult != null) {
                    SolutionRank solutionRank = solutionRankResult;
                    solutionRank.setId(solutionRankResult.getId());
                    solutionRank.setSolutionId(solutionRankReq.getSolutionId());
                    solutionRank.setRankType(solutionRankReq.getRankType());
                    solutionRank.setIsDel(DeleteEnum.NOT_DEL.getDbValue());
                    solutionRank.setModifiedTime(new Date());
                    solutionRank.setModifiedBy(solutionRankReq.getModifiedBy());
                    solutionRank.setOperator(solutionRankReq.getOperator());
                    solutionRankService.updateById(solutionRank);
                } else {
                    // 当solutionId变了
                    if (NumberHelper.validatePositive(solutionRankReq.getSolutionId())) {
                        SolutionRank solutionRank = new SolutionRank();
                        solutionRank.setSolutionId(solutionRankReq.getSolutionId());
                        solutionRank.setRankType(solutionRankReq.getRankType());
                        solutionRank.setIsDel(DeleteEnum.NOT_DEL.getDbValue());
                        solutionRank.setCreatedBy(solutionRankReq.getModifiedBy());
                        solutionRank.setCreatedTime(new Date());
                        solutionRank.setModifiedTime(new Date());
                        solutionRank.setModifiedBy(solutionRankReq.getModifiedBy());
                        solutionRank.setOperator(solutionRankReq.getOperator());
                        solutionRankService.insert(solutionRank);
                    }
                }

            }
        }
        return metaList;
    }

    /**
     * @Description: OMS_查询方案是否存在
     * @Author: liyi
     */
    public Solution getSolutionById(Long solutionId) {
        Solution solution = solutionService.findBySolutionId(solutionId);
        if (solution == null) {
            throw new BizException(ErrorCodeEnum.NotExist.getDbValue(), "不存在该方案");
        }
        return solution;
    }


    public BrandFirmsEntranceResp queryBrandFirmsEntrance() {
        BrandFirmsEntranceResp resp = new BrandFirmsEntranceResp();
        List<BrandOwnerBean> list = brandOwnerService.queryBrandFirmsEntrance();
        resp.setList(list);
        return resp;
    }


}
